package com.kk.controller;

import com.kk.config.WebSecurityConfig;
import com.kk.dao.UserDao;
import com.kk.model.Menu;
import com.kk.model.User;
import com.kk.service.MenuService;
import org.hibernate.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: kevin
 * @date: 2018-11-28 23:33
 */
@Controller
public class LoginController {
    //    private static final String PREFIX = "system/";

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserDao userDao;

    @GetMapping("/")
    public String index(Model model, HttpSession httpSession) {
        Object object = httpSession.getAttribute(WebSecurityConfig.SESSION_KEY);
        if (object!=null) {
            User user = userDao.findByAccount(object.toString());
            if (user != null) {
                //TODO 用户管理增价分配角色
                List<Menu> titles = menuService.getMenuTreeByUser(user);
                model.addAttribute("titles", titles);
                model.addAttribute("user", user);
            }
            return "index";
        }
        return "login";
    }

    @GetMapping("/index")
    public String indexPage(Model model, HttpSession httpSession) {
        Object object = httpSession.getAttribute(WebSecurityConfig.SESSION_KEY);
        if (object!=null) {
            User user = userDao.findByAccount(object.toString());
            if (user != null) {
                List<Menu> titles = menuService.getMenuTreeByUser(user);
                model.addAttribute("titles", titles);
                model.addAttribute("user", user);
            }
            return "index";
        }
        return "login";
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }
    @PostMapping("/login")
    public String login(@ModelAttribute User user, Model model, HttpSession httpSession) {
        String name = user.getName();
        String password = user.getPassword();
        if (StringHelper.isNotEmpty(name.trim()) && StringHelper.isNotEmpty(password.trim())) {
            User u = userDao.findByAccountAndPassword(name, password);
            if (u!=null) {
                User loginUser = userDao.findByAccount(name);
                List<Menu> titles = menuService.getMenuTreeByUser(loginUser);
                model.addAttribute("titles", titles);
                model.addAttribute("user", loginUser);
                httpSession.setAttribute(WebSecurityConfig.SESSION_KEY, user.getName());
                return "redirect:index";
            }

        }
        return "login";
    }

    @GetMapping("/logout")
    public String logout(HttpSession httpSession) {
        httpSession.removeAttribute(WebSecurityConfig.SESSION_KEY);
        return "redirect:login";
    }

}
